package com.xywy.bloodandroid.act;

import java.util.HashMap;
import java.util.regex.Pattern;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.User;
import com.xywy.bloodandroid.util.Constants;
import com.xywy.bloodandroid.util.UIUtil;
import com.xywy.bloodandroid.vollery.OnVolleyResponseListener;
import com.xywy.bloodandroid.vollery.PostRequest;

public class SetNewPasswordAct extends BaseActivity {
	private RelativeLayout left_img;
	private TextView middle_title;
	private EditText new_password;
	private EditText confirm_password;
	private RelativeLayout set_login_button;
	private String phonenum;
	private String code;
	private TextView chenck_password_text;
	private ImageView chenck_password_img;
	@Override
	protected void initView(Bundle savedInstanceState) {
     setContentView(R.layout.act_setnewpassword);
        left_img = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText(getResources().getString(R.string.set_new_password));
		new_password = (EditText )findViewById(R.id.new_password);
		confirm_password = (EditText )findViewById(R.id.confirm_password);
		set_login_button = (RelativeLayout)findViewById(R.id.set_login_button);
		phonenum = getIntent().getStringExtra("target");
		code = getIntent().getStringExtra("code");
		
		chenck_password_text = (TextView) findViewById(R.id.chenck_password_text);
		chenck_password_img = (ImageView) findViewById(R.id.chenck_password_img);
		
		new_password.addTextChangedListener(PsswordTextWatcher);
		new_password.setSelection(new_password.length());
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
		set_login_button.setOnClickListener(this);
	}

	@Override
	protected void initData() {

	}
  @Override
public void onClick(View v) {
	super.onClick(v);
	switch (v.getId()) {
	case R.id.left_img:
		finish();
		break;
	case R.id.set_login_button:
		String password=new_password.getText().toString().trim();
		String comfirmpassword=confirm_password.getText().toString().trim();
		if(IsPasswordSame(password,comfirmpassword))
		{
			confirmPassword(password,comfirmpassword); 
		}
		
		break;

	default:
		break;
	}
}
 
  private boolean IsPasswordSame(String password,String comfirmpassword){
	  if(password==null&&comfirmpassword==null){
		  UIUtil.showToast(this, "输入不能为空~"); 
		  return false; 
	  }
	  else if(password.equals(comfirmpassword))
	  {
		  return true;
	  }
	  else {
		 UIUtil.showToast(this, "两次输入的密码不一样~");
	     return false;
	  }
  }
private void confirmPassword(String password,String comfirmpassword ) {
   PostRequest<User> postRequest=new PostRequest<User>(Constants.getUrl("kbb", "resetProperty"), User.class, new OnVolleyResponseListener<User>() {

	@Override
	public void onError(VolleyError volleyError) {
		UIUtil.DissDialog(context, waittingDialog );
        UIUtil.showToast(context, getResources().getString(R.string.network_slow));
	}

	@Override
	public void onSuccess(User response) {
		UIUtil.DissDialog(context, waittingDialog );
		if(response!=null){
			if(response.getState()==200){
				Intent intent=new Intent(context, MainFragmentAct.class);
				startActivity(intent);
				finish();
			}else{
				UIUtil.showToast(context, response.getError());
			}
			
		}
	}
    });
   HashMap<String, String> params = new HashMap<String, String>();
	params.put("userid", User.getInstance(context).getUserid());
	params.put("phonenum", phonenum);
	params.put("type", "2");
	params.put("code", code);
	params.put("keyword", "password");
	params.put("value", password);
	postRequest.setParams(params);
	executeRequest(postRequest);
}

private TextWatcher PsswordTextWatcher=new TextWatcher() {
	private int editEnd;
	private int editStart;
	private CharSequence temp;
	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		temp=s;
	}
	
	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		
	}
	
	@Override
	public void afterTextChanged(Editable s) {
		editStart = new_password.getSelectionStart();
		editEnd = new_password.getSelectionEnd();
		new_password.removeTextChangedListener(PsswordTextWatcher);
			if(Pattern.matches(Constants.PASSWORD, temp)){
			chenck_password_img.setVisibility(View.VISIBLE);
			}else{
				chenck_password_img.setVisibility(View.GONE);
			}
		if (temp.length()!=0) {
			chenck_password_text.setVisibility(View.GONE);
		}else{
			chenck_password_text.setVisibility(View.VISIBLE);
		}
		
		new_password.setText(s);
		new_password.setSelection(editStart);
		
		new_password.addTextChangedListener(PsswordTextWatcher);
	}
};
}
